package com.boarsoft.boar.gateway;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.common.Util;

public class GatewayConfig {
	private static final Logger log = LoggerFactory.getLogger(GatewayConfig.class);

	/** */
	public static final Properties prop = new Properties();

	/** */
	public static int TIMEOUT = 30000;

	public static synchronized void init(InputStream is) {
		try {
			prop.load(is);
		} catch (IOException e) {
			log.error("Error on load system config.", e);
			throw new RuntimeException(e);
		}
	}

	public static void config() {
		ClassLoader cl = GatewayConfig.class.getClassLoader();
		try {
			// 读取主配置
			log.info("Load conf/gateway.properties ...");
			synchronized (prop) {
				prop.clear();
				prop.load(cl.getResourceAsStream("conf/gateway.properties"));
			}
			// 转发超时设置
			TIMEOUT = getInt("timeout", TIMEOUT);
		} catch (IOException e) {
			log.error("Error on load conf/gateway.properties.", e);
		}
	}

	public static int getInt(String key, int value) {
		return Util.str2int(prop.getProperty(key), value);
	}

	public static String getProperty(String key, String value) {
		return prop.getProperty(key, value);
	}

	public static Properties getProperties() {
		return prop;
	}
}
